package org.sjs.dreamfly.common.base

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import kotlinx.coroutines.MainScope
import org.sjs.dreamfly.common.util.LogUtil
import org.sjs.dreamfly.widget.TitleBar

abstract class BaseFragment: Fragment(), UiInterface {
    protected lateinit var loadingView: View
    protected var titleBar: TitleBar? = null
    protected val mainScope by lazy { MainScope() }
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        LogUtil.d("${this::class.java}-${hashCode()}")
        requireLayoutId()?.let {
            return inflater.inflate(it, container, false)
        }
        return super.onCreateView(inflater, container, savedInstanceState)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        onInitViewModel()
        onBindViews()
        onInitViews()
        onLoadData()
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        LogUtil.d("onAttach")
    }

    override fun onDetach() {
        LogUtil.d("onDetach")
        super.onDetach()
    }

    open fun onBackPressed(): Boolean { return false }

    open fun hideLoadingView() {
        if (isAdded && !isDetached) {
            (requireActivity() as? BaseActivity)?.hideLoadingView()
        }
        loadingView.visibility = View.GONE
    }
    open fun showLoadingView() {
        if (isAdded && !isDetached) {
            (requireActivity() as? BaseActivity)?.hideLoadingView()
        }
        loadingView.visibility = View.VISIBLE
    }

    fun hideTitleBar() {
        titleBar?.visibility = View.GONE
    }

    fun showTitleBar() {
        titleBar?.visibility = View.VISIBLE
    }
}